package de.jmda.mview.fx.node.typeshape;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javafx.geometry.HPos;
import javafx.scene.control.Label;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;

import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;

import de.jmda.mproc.ProcessingUtilities;
import de.jmda.mview.typeshape.DisplayOptionsModelUtil;

public class Methods
    extends VBox
{
//	private final static Logger LOGGER = Logger.getLogger(Methods.class);
//
//	private TypeElement typeElement;
//	private DisplayOptionsModelUtil displayOptionsModelUtil;#
	
	private Label header;
	
	private Map<String, ElementView> methodViewMap =
			new HashMap<String, ElementView>();

	/**
	 * sorted set of methods
	 */
	private Set<ElementData<ExecutableElement>> methodDataSet =
			new TreeSet<ElementData<ExecutableElement>>(
					new Comparator<ElementData<ExecutableElement>>()
					{
						@Override
		        public int compare(
		        		ElementData<ExecutableElement> o1,
		        		ElementData<ExecutableElement> o2)
		        {
			        return o1.getName().compareTo(o2.getName());
		        }
					});

	public Methods(
			TypeElement typeElement, DisplayOptionsModelUtil displayOptionsModelUtil)
	{
		super();

//		this.typeElement = typeElement;
//		this.displayOptionsModelUtil = displayOptionsModelUtil;

		for (ExecutableElement method : ProcessingUtilities.getMethods(typeElement))
		{
			ElementData<ExecutableElement> methodData =
					new ElementData<ExecutableElement>(method, displayOptionsModelUtil);

			methodDataSet.add(methodData);
			methodViewMap.put(
					method.toString(),
					new MethodView(methodData, displayOptionsModelUtil));
		}
		
		GridPane gridPane = new GridPane();

		int i = 0;

		gridPane.setHgap(2);

		for (ElementData<ExecutableElement> methodData : methodDataSet)
		{
			ElementView methodView =
					methodViewMap.get(methodData.getElement().toString());

			gridPane.add(methodView.getIcon(), 0, i);
			gridPane.add(methodView.getType(), 1, i);
			gridPane.add(methodView.getName(), 2, i);

			GridPane.setHalignment(methodView.getType(), HPos.RIGHT);

			i++;
		}

		ColumnConstraints columnConstraints = new ColumnConstraints();
		columnConstraints.setHalignment(HPos.RIGHT);

		gridPane.getColumnConstraints().add(0, columnConstraints);

		header = new Label("methods");
		header.setMaxWidth(Double.MAX_VALUE); // makes header span the whole width of typeshape (vbox)
		header.getStyleClass().add("fields-header");

		getChildren().add(header);
		getChildren().add(gridPane);
	}
}